package org.jsbd.boss.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.res.AXmlResourceParser;


public class ManifestUtils {
	public static String readFile(String fileName) {
		StringBuilder sb = new StringBuilder();
		try {
			File file = new File(fileName);
			InputStreamReader read = new InputStreamReader(new FileInputStream(file));
			BufferedReader bufferedReader = new BufferedReader(read);
			String text = null;
			while ((text = bufferedReader.readLine()) != null) {
				sb.append(text);
			}
			bufferedReader.close();
		} catch (Exception e) {
		}
		return sb.toString();
	}
	public static HashMap<String, String> getManifestInfo(String fileName) {
		String manifest = readFile(fileName);
		HashMap<String, String> info = new HashMap<String, String>();
		try {
			XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
			parser.setInput(new StringReader(manifest));
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				String nodeName = parser.getName();
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if (nodeName.equals("manifest")) {
						int count = parser.getAttributeCount();
						for (int i = 0; i < count; i++) {
							String attributeName=parser.getAttributeName(i);
							if("android:versionCode".equals(attributeName)){
								info.put("versionCode", parser.getAttributeValue(i));
							}
							if("android:versionName".equals(attributeName)){
								info.put("versionName", parser.getAttributeValue(i));
							}
							if("package".equals(attributeName)){
								info.put("package", parser.getAttributeValue(i));
							}
						}
					}
					if (nodeName.equals("activity")) {
						int count = parser.getAttributeCount();
						for (int i = 0; i < count; i++) {
							String attributeName=parser.getAttributeName(i);
							if("android:name".equals(attributeName)){
								String main_activity=parser.getAttributeValue(i);
								if(!main_activity.contains(".")){
									info.put("mainActivity",info.get("package")+"."+ main_activity);
								}else{
									if(main_activity.startsWith(".")){
										info.put("mainActivity",info.get("package")+main_activity);
									}else{
										info.put("mainActivity",main_activity);
									}
								}
								
							}
						}
					}
					if (nodeName.equals("action")) {
						int count = parser.getAttributeCount();
						for (int i = 0; i < count; i++) {
							String attributeName=parser.getAttributeName(i);
							if("android:name".equals(attributeName)){
								String main=parser.getAttributeValue(i);
								if(main.equals("android.intent.category.LAUNCHER")){
									return info;
								}
							}
						}
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return info;
	}
	
	public static HashMap<String, String> parseAxml(InputStream is)
			throws Exception {
		HashMap<String, String> info = new HashMap<String, String>();
		AXmlResourceParser parser = new AXmlResourceParser();
		parser.open(is);
		int type = 0;
		do {
			type = parser.next();
			String nodeName = parser.getName();
			switch (type) {
			case XmlPullParser.START_DOCUMENT:
				break;
			case XmlPullParser.START_TAG:
				if (nodeName.equals("manifest")) {
					int count = parser.getAttributeCount();
					for (int i = 0; i < count; i++) {
						String attributeName = parser.getAttributeName(i);
						if ("versionCode".equals(attributeName)) {
							info.put("versionCode", String.valueOf(parser.getAttributeIntValue(i, 0)));
						}
						if ("versionName".equals(attributeName)) {
							info.put("versionName", parser.getAttributeValue(i));
						}
						if ("package".equals(attributeName)) {
							info.put("package", parser.getAttributeValue(i));
						}
					}
				}
				if (nodeName.equals("activity")) {
					int count = parser.getAttributeCount();
					for (int i = 0; i < count; i++) {
						String attributeName = parser.getAttributeName(i);
						if ("name".equals(attributeName)) {
							String main_activity = parser.getAttributeValue(i);
							if (!main_activity.contains(".")) {
								info.put("mainActivity", info.get("package")
										+ "." + main_activity);
							} else {
								if (main_activity.startsWith(".")) {
									info.put("mainActivity",
											info.get("package") + main_activity);
								} else {
									info.put("mainActivity", main_activity);
								}
							}

						}
					}
				}
//				if (nodeName.equals("action")) {
//					int count = parser.getAttributeCount();
//					for (int i = 0; i < count; i++) {
//						String attributeName = parser.getAttributeName(i);
//						if ("name".equals(attributeName)) {
//							String main = parser.getAttributeValue(i);
//							if (main.equals("android.intent.category.LAUNCHER")) {
//								// return info;
//							}
//						}
//					}
//				}
				if(info.size() >= 4){	//读取的大小大于等于4，表示读取完全了
					return info;
				}
				break;
			}
		} while (type != XmlPullParser.END_DOCUMENT);
		return info;
	}
	
}
